﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.DAL.DAOs;

namespace LibrarySystem.Controllers.LibraryControllers
{
    public class StudentController : Controller
    {
        Library_Students libStu = new Library_Students();


        public ActionResult LibraryStudent()
        {
            return View();
        }

        [HttpPost]
        public JsonResult StudentOptions(string code = null)
        {
            try
            {
                List<StudentModel> stuModel = libStu.getStuToComboBox(code);
                var stuOptions = stuModel.Select(s => new { Value = s.ID, DisplayText = s.StudentCode });

                return Json(new { Result = "OK", Options = stuOptions });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult GetStudentList(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null,
            string studentCode = null, string name = null, string classID = null)
        {
            try
            {
                int count = libStu.GetDataCount();
                List<StudentModel> stuList = libStu.GetDataList(jtStartIndex, jtPageSize, jtSorting, studentCode, name, classID);
                return Json(new { Result = "OK", Records = stuList, TotalRecordCount = count });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult AddStudent(StudentModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                if (libStu.CheckStudentRoll(model.StudentCode))
                {
                    return Json(new { Result = "Error", Message = "Mã số SV đã tồn tại! Xin nhập lại!" });
                }
                var add = libStu.AddNewData(model);
                return Json(new { Result = "OK", Record = add });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult UpdateStudent(StudentModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libStu.UpdateData(model);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult DeleteStudent(int Id)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libStu.DeleteData(Id);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

    }
}
